<?php

Class FWregistry
{
    protected static $instance = null;

    var $var = array();
    var $objects = array();

    /**
     *  Instance our Registry
     * @return FWregistry::$instance;
     */

    public static function getInstance()
    {
        if(self::$instance === null)
        {
            self::$instance = new FWregistry();
        }
        return self::$instance;
    }

    /**
     * no custom registries
     * no anarchy
     */
    private function __clone() {}
    protected function __construct() {
        
        // Init Session handling into registry

        session_start();

        if(!isset($_SESSION["__r"]))
        {
            $_SESSION["__r"] = array();
        }

    }


    /**
     *  SIMPLE SET
     * @todo extend to multi array with path (+path rights)
     * @param string $index
     * @param $value
     */
    public function set($key, $value, $namespace = "default")
    {
         if( !isset( $this->var[$namespace] ) )
         {
            $this->var[$namespace] = array();
         }
        $this->var[$namespace][$key]["data"] = $value;
    }
    public function __set($key, $value)
    {
        return $this->set($key, $value);
    }
    /**
     * SIMPLE GET
     *  returns a stored registry value by key
     * @param string $index
     * @return $value
     */
    public function get($key, $namespace = "default")
    {
        if(isset( $this->var[$namespace][$key]["data"] ))
        {
            return $this->var[$namespace][$key]["data"];
        }
        return null;
    }
    public function __get($key)
    {
        return $this->get($key);
    }
    /**
     * SESSION SET
     * 
     */
     public function sset($key, $value, $namespace = "default")
     {
         if( !isset( $_SESSION["__r"][$namespace] ) )
         {
            $_SESSION["__r"][$namespace] = array();
         }
         $_SESSION["__r"][$namespace][$key]["data"] = $value;
     }
    /**
     * SESSION GET
     *
     */
     public function sget($key, $namespace = "default")
     {

        if(isset( $_SESSION["__r"][$namespace][$key]["data"] ))
        {
            return $_SESSION["__r"][$namespace][$key]["data"];
        }
        return null;
     }


    /**
     *  handles registry objects
     *
     * can be extendet example artist
     *
     * @param string $objClass
     * @param string $objId
     * @return object
     */
    function object($objClass, $objId)
    {
        $objClass = (string)$objClass;
        /*
         * search for object in registry.
         *      ifset -> return from registry
         */
        if(isset($this->objects[$objClass][(string)$objId]))
        {
            return $this->objects[$objClass][(string)$objId];
        }
        /**
         * initialize FWobject extendet object
         * @todo validate Object instanceof
         */
        $obj = new $objClass();

        /**
         * Configurations
         */
        $obj->setId((string)$objId);

        /**
         * send object reference to registry
         */
        $this->objects[$objClass][(string)$objId] = $obj;
        /**
         * boot object
         * runn the custom init() function
         */
        $obj->boot();


        return $obj;
    }

    /**
     *
     * artist object handling
     *
     * extendes FWobjects handling to simple fire $r->aritst("Fleshgordon");
     *
     */
     function artist($name)
     {
         return $this->object("artist", $name);
     }

     
     function registerController($obj, $action = "index()")
     {

     }
     function registerPreAction($obj)
     {

     }
     function runActionStack($obj)
     {

     }
     function runControllerStack($obj)
     {
         
     }
}

?>
